// 引入用来发送请求的方法，防止回调地狱 
import { request } from '../../request/index.js'

Page({
  data: {
    scrollTop: 0,
    // 当前的分类，-1 为"全部"分类，其它列表从 0 开始
    curTag: 0,
    // 轮播图数组 
    swiperList: [],
    tags: ['标签1', '标签2', '标签3', '标签4', '标签5', '标签6', '标签7'],
    goodsList: [{
      goods_id: 1,
      goods_title: "二手书",
      goods_description: "二手书111",
      goods_pic: "https://api-hmugo-web.itheima.net/pyg/banner2.png",
      goods_price: "100.00"
    }, {
      goods_id: 2,
      goods_title: "二手书",
      goods_description: "二手书111",
      goods_pic: "https://api-hmugo-web.itheima.net/pyg/banner2.png",
      goods_price: "100.00"
    }, {
      goods_id: 3,
      goods_title: "二手书",
      goods_description: "二手书111",
      goods_pic: "https://api-hmugo-web.itheima.net/pyg/banner2.png",
      goods_price: "100.00"
    }
    ],
  },

  // 接口要的参数
  QueryParams: {
    cid: "电子", // 分类的标签
    pageNum: 1,
    pageSize: 12
  },
  // 商品列表的总页数
  totalPages: 1,

  onLoad: function () {
    this.init()
  },
  async init() {
    await this.getSwiperList();
    await this.getTagList();
    await this.getGoodList();
  },
  // 标签序号转为标签字符串
  tagToString() {
    if (this.data.curTag == -1) {
      this.QueryParams.cid = '全部'
    }
    this.QueryParams.cid = this.data.tags[this.data.curTag];
  },
  // 获取商品列表参数
  async getGoodList() {
    this.tagToString()
    console.log(this.QueryParams)
    const result = await request({
      url: '/secondhand/goods/getGoodsByLabel',
      // data: this.QueryParams,
      data: {
        cid: "电子", // 分类的标签
        pageNum: 1,
        pageSize: 12
      },
      method: 'GET',
    })
    console.log(result)
    let array = result.data.obj.map(item => ({
      goods_id: item.id,
      goods_title: item.goodName,
      goods_description: item.description,
      goods_pic: item.imageURL,
      goods_price: item.price
    }))
    this.setData({
      // 拼接数组
      goodsList: [...this.data.goodsList, ...array]
    })
    //关闭下拉效果
    wx.stopPullDownRefresh()

    // const result = await request({ url: 'http://47.102.99.215:9090/secondhand/goods/getGoodsByLabel', data: this.QueryParams })
    // const { total } = result.data.message;
    // // 计算总页数
    // this.totalPages = Math.ceil(total / this.QueryParams.pagesize),
    //   this.setData({
    //     // 拼接数组
    //     goodsList: [...this.data.goodsList, ...result.data.message.goods]
    //   })
    // // 关闭下拉效果
    // wx.stopPullDownRefresh()
  },
  async getTagList() {
    const result = await request({
      url: '/secondhand/goods/getAllLabel',
    })
    this.setData({
      tags: result.data.obj
    })
  },
  // 获取轮播图数据
  async getSwiperList() {
    // 测试用，后端数据不规范，先用这个，规范之后再改回去
    const result = await request({ url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata' })
    // request({ url: '/secondhand/goods/swiper' })
    this.setData({
      swiperList: result.data.message
    })
  },
  //监测屏幕滚动
  onPageScroll: function (e) {
    this.setData({
      scrollTop: parseInt((e.scrollTop) * wx.getSystemInfoSync().pixelRatio)
    })
  },
  // 选择 "全部" 标签
  selectAll: function (e) {
    this.setData({
      curTag: -1
    });
    this.getGoodList();
  },
  // 选择其他的分类标签
  tagSelect: function (e) {
    this.setData({
      curTag: e.currentTarget.dataset.id
    })
    this.getGoodList();
  },
  //跳转详情
  detail: function (e) {
    let that = this;
    wx.navigateTo({
      url: '/pages/detail/detail?scene=' + e.currentTarget.dataset.id,
    })
  },
  // 触底事件
  // onReachBottom() {
  //   // 如果没有下一页
  //   if (this.QueryParams.pagenum >= this.totalPages) {
  //     console.log(没有下一页了),
  //       wx.showToast({
  //         title: '没有下一页数据了',
  //       })
  //   }
  // 有下一页则再次请求数据
  // else {
  //   this.QueryParams.pagenum++;
  //   this.getGoodList();
  // }
  // },
  // 下拉事件
  onPullDownRefresh() {
    // 重置数组
    this.setData({
      goodsList: []
    });
    // 重置页码
    this.QueryParams.pagenum = 1;
    // 发送请求
    this.getGoodList();
  }
})